package com.lkd.shoppingcart.service.impl;

import java.util.List;

import com.lkd.shoppingcart.dao.CartDao;
import com.lkd.shoppingcart.dao.OrderDao;
import com.lkd.shoppingcart.dao.OrderDetailDao;
import com.lkd.shoppingcart.dao.ProductDao;
import com.lkd.shoppingcart.dao.impl.CartDaoImpl;
import com.lkd.shoppingcart.dao.impl.OrderDaoImpl;
import com.lkd.shoppingcart.dao.impl.OrderDetailDaoImpl;
import com.lkd.shoppingcart.dao.impl.ProductDaoImpl;
import com.lkd.shoppingcart.entity.Cart;
import com.lkd.shoppingcart.entity.CartItemView;
import com.lkd.shoppingcart.entity.OrdersDetail;
import com.lkd.shoppingcart.entity.OrdersView;
import com.lkd.shoppingcart.entity.Product;
import com.lkd.shoppingcart.service.OrderService;

public class OrderServiceImpl implements OrderService {
	
	private OrderDao orderDao = new OrderDaoImpl();
	private OrderDetailDao orderDetailDao = new OrderDetailDaoImpl();
	private ProductDao productDao = new ProductDaoImpl();
	private CartDao cartDao = new CartDaoImpl();

	@Override
	public String addOrder(Integer userId, Integer productId, Integer quantity) {
		// 保证订单号的唯一性
        String orderId = System.currentTimeMillis()+""+userId;
        
		orderDao.addOrder(orderId, userId);
		orderDetailDao.addOrderDetail(orderId, productId, quantity);
		
		// 更新库存和销量
		Product product = productDao.queryById(productId);
		product.setSales(product.getSales() + quantity);
		product.setCounts(product.getCounts() - quantity);
		productDao.updateProduct(product);
		return orderId;
	}
	
	@Override
	public String addOrder(Cart cart) {
		// 保证订单号唯一性
        String orderId = System.currentTimeMillis()+""+ cart.getUserId();
        
        orderDao.addOrder(orderId, cart.getUserId());
        for (CartItemView cartItemView : cart.getItems()) {
        	
        	orderDetailDao.addOrderDetail(orderId, cartItemView.getProductId(), cartItemView.getQuantity());
        	
        	// 更新库存和销量
        	Product product = productDao.queryById(cartItemView.getProductId());
    		product.setSales(product.getSales() + cartItemView.getQuantity());
    		product.setCounts(product.getCounts() - cartItemView.getQuantity());
    		productDao.updateProduct(product);
    		
    		// 从购物车中移除
    		cartDao.removeById(cartItemView.getUserId(), cartItemView.getProductId());
    		
        }
        return orderId;
	}

	@Override
	public void updateStatusById(String orderId, Integer status) {
		orderDao.updateStatusById(orderId, status);
	}
	
	@Override
	public void updateStatus(Integer oldStatus, Integer newStatus) {
		orderDao.updateStatus(oldStatus, newStatus);
	}

	@Override
	public List<OrdersView> queryByUserId(Integer userId) {
		return orderDao.queryByUserId(userId);
	}
	
	@Override
	public List<OrdersView> queryAll() {
		return orderDao.queryAll();
	}

	@Override
	public List<OrdersDetail> queryForDetail(String orderId) {
		return orderDetailDao.queryByOrderId(orderId);
	}
	
	@Override
	public int checkStock(Cart cart) {
		for (CartItemView cartItemView : cart.getItems()) {
			Product product = productDao.queryById(cartItemView.getProductId());
			if (product.getCounts() < cartItemView.getQuantity()) {
				return cartItemView.getProductId();
			}
		}
		return 0;
	}

}
